package com.kylin.module.ajax.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.List;
import java.util.Locale;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import com.kylin.common.paginate.Paging;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kylin.common.Constants;
import com.kylin.module.community.business.SitemsgBlogic;

import com.kylin.module.user.orm.User;
import com.kylin.hibernate.BaseHibernateDAO;
//import com.kylin.module.admin.form.CatDictForm;
import com.kylin.module.community.business.PostBlogic;

import com.kylin.module.community.orm.Post;

import com.kylin.module.community.forms.PostForm;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

import com.kylin.common.exception.BusinessException;
import com.kylin.common.util.MemObjFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONString;



public class AjaxAction  extends BaseDispatchAction {

	protected static Logger log = Logger.getLogger(AjaxAction.class);	
	
	public ActionForward unspecified(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		return null;
	}
	
	public ActionForward username(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response){
		log.info("in username");
		//String forward = null;
		
		String value = request.getParameter("value");		
		response.setCharacterEncoding("utf-8");
		PrintWriter out=null;
		try{
			out=response.getWriter();			   
		}catch(IOException ioe){
			log.info("ioe:" + ioe.toString());
		}		
		String jsonString =  null;
		List list = null;
		try
		{
			list = BaseHibernateDAO.findByProperty(User.class, "username", value.toLowerCase());
		}
		catch(BusinessException be){
			log.info(be.toString());
		}

		JSONObject json = new JSONObject();
	
		try{			
			json.put("id", "username");
			json.put("value", value);
			if (list != null){
				if (list.size() == 0){
					json.put("success", true);
					json.put("msg", "OK");
				}else{
					json.put("success", false);
					json.put("msg", "Please choose another Username");
				}
			}			
			
		}
		catch(JSONException je){
			
		}
		
		jsonString =  json.toString();
		//jsonString = "{ id: 'username', value: '" + value + "', success: " + canUse + ", msg: '" + msg + "'}"; 	
		System.out.println(jsonString);
		if (out != null){
			out.write(jsonString);
		}
	    return null;
	}	
	
	public ActionForward email(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response){
		log.info("in email");
		//String forward = null;
		
		String value = request.getParameter("value");		
		response.setCharacterEncoding("utf-8");
		PrintWriter out=null;
		try{
			out=response.getWriter();			   
		}catch(IOException ioe){
			log.info("ioe:" + ioe.toString());
		}		
		String jsonString =  null;

		List list = null;
		try
		{
			list = BaseHibernateDAO.findByProperty(User.class, "email", value.toLowerCase());
		}
		catch(BusinessException be){
			log.info(be.toString());
		}

		JSONObject json = new JSONObject();
		
		try{			
			json.put("id", "email");
			json.put("value", value);
			if (list != null){
				if (list.size() == 0){
					json.put("success", true);
					json.put("msg", "OK");
				}else{
					json.put("success", false);
					json.put("msg", "Please choose another Email");
				}
			}			
			
		}
		catch(JSONException je){
			
		}
			
		jsonString = json.toString(); 	
		System.out.println(jsonString);
		if (out != null){
			out.write(jsonString);
		}

	    return null;
	}

}
